<template>
  <div class="login jx-flex jx-center">
    <div class="container">
      <h2 class="jx-margin">WELCOME</h2>
      <div class="input-c">
        <el-input
          prefix-icon="el-icon-user"
          v-model="account"
          placeholder="用户名"
          clearable
        />
      </div>
      <div class="input-c">
        <el-input
          type="password"
          v-model="pwd"
          prefix-icon="el-icon-lock"
          placeholder="密码"
          clearable
          class="jx-mar-top"
        />
      </div>
      <el-button class="submit jx-mar-top" type="primary" @click="submit"
        >登陆</el-button
      >
      <p class="account jx-mar-top"></p>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import { Request } from "@/utils/request.ts";
import { ElMessage,ElLoading } from "element-plus";
interface StateType {
  id: number;
  name: String;
  account: String;
  pwd: String;
  isShowLoading: boolean;
}

const Component = defineComponent({
  components: {},
  props: {},
  data(): StateType {
    return {
      id: 1,
      name: "hahah",
      account: "",
      pwd: "",
      isShowLoading: false,
    };
  },
  created(){
    document.body.addEventListener("keydown",(e)=>{
      if(e.keyCode === 13){
        this.submit();
      }
    })
  },
  methods: {
    submit(): void {
      const loading =  ElLoading.service({
        text:"登录中..."
      })
      Request("/login","POST",{ user_name: this.account, user_psd: this.pwd },{}).then((res) => {
        const { token, permissions, msg }: any = res;
        if (token) {
          window.localStorage.setItem("token", token);
          window.localStorage.setItem(
            "permissions",
            JSON.stringify(permissions)
          );
          this.$router.push("/");
        }
        loading.close();
      })
    },
  },
  destroyed(){
    document.body.removeEventListener('keydown',(e)=>{});
  }
});

export default Component;
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  background: url("https://picsum.photos/1920/1080") no-repeat;
}
</style>
